package com.hlkj.pay.app.common.ext;

import cn.hutool.core.bean.BeanUtil;
import com.hlkj.framework.common.util.json.JsonUtils;
import com.hlkj.pay.vo.common.req.MessageSendParamsReq;
import org.springframework.util.StringUtils;

import java.util.Map;

/**
 * @author zhou.long
 * 
 * @date 2021/07/30 18:43
 */
public class MessageUtil {

    /**
     * set to array
     * @return
     */
    public static String[] templates(String baseContent, String params) {
        if (!StringUtils.hasText(params)) {
            return new String[0];
        }
        MessageSendParamsReq messageSendParamsReq = JsonUtils.parseObject(params, MessageSendParamsReq.class);
        
        if (StringUtils.isEmpty(baseContent) || !baseContent.contains("{")) {
            return new String[0];
        }
        String[] split = baseContent.split("\\{");
        String[] arrays = new String[split.length - 1];
        int i = 0;
        Map<String, Object>  messageSendParamMap = BeanUtil.beanToMap(messageSendParamsReq);
        int indexOf;
        for (String key : split) {
            indexOf = key.indexOf("}");
            if (indexOf <= 1) {
                continue;
            }
            key = key.substring(0, indexOf);
            Object value = messageSendParamMap.get(key);
            if (value == null) {
                arrays[i] = "";
            } else {
                arrays[i] = value.toString();
            }
            i += 1;
        }
        return arrays;
    }
}
